package com.sky.chain.service.impl;

import com.sky.chain.data.GoodsData;
import com.sky.chain.service.IProduct;
import com.sky.chain.model.GoodsEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductA implements IProduct {
    @Override
    public void outAllGoodsInfo() {
        for (GoodsEntity goodsEntity : GoodsData.getInstance()) {
            System.out.println(goodsEntity);
        }
    }

    @Override
    public List<String> getShopNums() {
        List<String> shopNums = new ArrayList<>();
        for (GoodsEntity goodsEntity : GoodsData.getInstance()) {
            shopNums.add(goodsEntity.getShopName() + "剩余数量：" +goodsEntity.getShopNum() + "盒");
        }
        return shopNums;
    }

    @Override
    public Map<Integer, GoodsEntity> getShopNames() {
        int count = 0;
        Map<Integer, GoodsEntity> map = new HashMap<>();
        System.out.print("请选择购买得药品（");
        for (GoodsEntity goodsEntity : GoodsData.getInstance()) {
            System.out.print(++count +"."+goodsEntity.getShopName());
            map.put(count, goodsEntity);
        }
        System.out.print("） 输入99返回首界面");
        System.out.println();
        return map;
    }

    @Override
    public void updateGoods(String name, int number) {
        for (int i = 0; i < GoodsData.goodsDataList.size(); i++) {
            if (name.equals(GoodsData.goodsDataList.get(i).getShopName())){
                GoodsData.goodsDataList.get(i).setShopNum( GoodsData.goodsDataList.get(i).getShopNum() - number);
                GoodsData.goodsDataList.set(i, GoodsData.goodsDataList.get(i));
            }
        }
    }

    @Override
    public  void insertMoney(int money) {
        GoodsData.getUserInstance().setMoney(GoodsData.getUserInstance().getMoney() + money);
        System.out.println("充值成功！你的余额为：" + GoodsData.getUserInstance().getMoney() + "元");
    }

    @Override
    public boolean insertShop(String shopName, int number) {
        boolean flag = false;
        for (int i = 0; i < GoodsData.goodsDataList.size(); i++) {
            if (shopName.equals(GoodsData.goodsDataList.get(i).getShopName())){
                flag = true;
                GoodsData.goodsDataList.get(i).setShopNum( GoodsData.goodsDataList.get(i).getShopNum() + number);
                GoodsData.goodsDataList.set(i, GoodsData.goodsDataList.get(i));
                break;
            }
        }
        if (flag){
            System.out.println("补货成功！");
            outAllGoodsInfo();
            return flag;
        }else {
            System.out.println("补货失败！");
            return flag;
        }


    }

    @Override
    public void login() {
        System.out.println("密码正确，目前的余额为：" + GoodsData.getUserInstance().getMoney() + "元");
        for (com.sky.chain.model.GoodsEntity goodsEntity : GoodsData.goodsDataList) {
            System.out.println(goodsEntity.getShopName() + "剩余数量：" + goodsEntity.getShopNum() +"盒");
        }
        System.out.println("是否进入充值操作？(1.是 2.否)");
    }


}
